#include "stdafx.h"
#include "CViewImageControl.h"
#include "CLogFile.h"
#include <stdio.h>


IMPLEMENT_DYNAMIC(CViewImageControl, CWnd)

BEGIN_MESSAGE_MAP(CViewImageControl, CWnd)
	ON_WM_MOUSEMOVE()

//	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

CString strFileType[]= {L"JPG",L"PNG",L"BMP"};


CViewImageControl::CViewImageControl(void)
{
	//m_lstImageControl = 0;
	m_lstImageControl = new CList<CImageControl*>;

	m_nIndexView = 0;
	//	CList <CImageControl*> m_lstImageControl; 
	

}
CViewImageControl::~CViewImageControl(void)
{
	//// Make sure that there is no cell element in board game
	//while (m_plstImageControl.GetCount()>0) 
 //   {
	//	// Get one cell in board game
 //       CImageControl* temp = m_plstImageControl.RemoveTail();
	//	// delete
 //       delete temp;
 //       OutputDebugString(_T("Free List Element\n"));
 //   }
	////if m_lstImageControl
}
void CViewImageControl::SetNumberOfImage( int numberOfImage )
{
	m_nNumOfImage		= numberOfImage;
}
//CString strFileType[]= {L"JPG",L"PNG",L"BMP"};

void CViewImageControl::FindImageInPath( CString PathFolderImage )
{
	CString pathTypeImage = L"";
	WIN32_FIND_DATA fdJPG;  
	HANDLE handleJPG;
	FILE * m_pLogFile;
	fopen_s (&m_pLogFile, DEF_LOG_FILE_NAME,"a+");
	for ( int i = 0; i < 3; i++)
	{
		pathTypeImage.Format(L"%s\\*.%s",PathFolderImage,strFileType[i]);

		handleJPG =:: FindFirstFile (pathTypeImage ,&fdJPG);
		if(handleJPG!= INVALID_HANDLE_VALUE)
		{
			do {  

			//	CLogFile logfile;
				if ( fdJPG.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY )  
					continue;  
				TRACE ("%s\n", fdJPG.cFileName);  
			//	std::wstring ws(fdJPG.cFileName);
				//CString FileName(ws.c_str());
				CString FileName(fdJPG.cFileName);
				CString PathFolder_FileName; 
				PathFolder_FileName = L"";

				PathFolder_FileName.Format(L"%s\\%s",PathFolderImage,FileName);
				m_plstImageName.AddHead( PathFolder_FileName );
			} 
			while (:: FindNextFile (handleJPG, &fdJPG)); 

		}
	}
	if( m_pLogFile != 0 )
		fclose (m_pLogFile);
	:: FindClose (handleJPG);
}

void CViewImageControl::SetSizeOfImageControl( int Height, int Width )
{
	m_nWidthControl		= Width;
	m_nHeightControl	= Height;
}
int CViewImageControl::GetNumOfImage()
{
	return this->m_nNumOfImage;
}
int CViewImageControl::GetViewImageControlWidth()
{
	return this->m_nWidthControl;
}
int CViewImageControl::GetViewImageControlHeight()
{
	return this->m_nHeightControl;
}

void CViewImageControl::LoadViewImage( CString pathFolderImage )
{
	m_strPathFolderImage = pathFolderImage;
	FindImageInPath( pathFolderImage );
	DrawImage();
}

void CViewImageControl::DrawImage()
{
	CRect rect(5,5,60,75);

	//for(int i = 0 ; i< m_plstImageName.GetCount() ; i++)
	//CreateImageFromFile
	//	for ( auto ImageName = m_plstImageName.b
	//{// Get first cell position
	POSITION pos = m_plstImageName.GetHeadPosition();

	
	while(pos)
	{		
		/* get next cell */
		CString cur = (CString)m_plstImageName.GetNext( pos );
		CImageControl *pTem = new CImageControl;
		m_lstImageControl->AddHead( pTem );

		pTem->Create(	_T(""),
						WS_CHILD | WS_VISIBLE | BS_ICON | SS_NOTIFY,
						rect,
						this,
						1);
		//rc.right-rc.left), (INT)(rc.bottom-rc.top));
		pTem->SetImageHeight(rect.bottom - rect.top );
		pTem->SetImageWidth(rect.right - rect.left );
		pTem->CreateImageFromFile( cur );
		rect.left += 60 ;
		rect.top = 5 ;
		rect.right += 60;
		rect.bottom = 75;
	
	}
}

// Event handler

//void CViewImageControl::OnMouseMove(UINT nFlags, CPoint point)
//{
//	//BoradCellImage->ScrollWindow(-50,0);
//	ScrollWindow( -50, 0 );
//}